



#include <string>
#include <iostream>
#include <fstream>
#include <sys/stat.h>

#include "EmCache.h"
#include "ColorfulLog.h"
#include <armadillo>
#define DEBUG

EmCache::EmCache()
{
    fileName = "";
    en_flag = 0;
    refreshCache = 0;
}

EmCache::EmCache(std::string fileName, bool en_flag, bool refreshCache):
                fileName(fileName),
                en_flag(en_flag),
                refreshCache(refreshCache)
{
    // 使用缓存机制
    if(en_flag){
        // 判断文件是否存在
        struct stat buffer;   
        if (stat(fileName.c_str(), &buffer) != 0){
            // 文件不存在，创建一个空文件
            std::ofstream file(fileName);
            // file << "This is a new file created by the program.";
            if (file.is_open()) {
                #ifdef DEBUG
                // printf("Creat file:%s\n",fileName.c_str());
                std::cout << "Creat file:\"" << fileName << "\"" << std::endl;
                #endif // DEBUG
                file.close();
            } 
            else {
                #ifdef DEBUG
                std::cerr << "Failed to create file:\"" << fileName << "\"" << std::endl;
                #endif // DEBUG
            }
        }
    }

}

EmCache::~EmCache()
{
}

bool EmCache::save(void* data, uint8_t length){
    char *data_chPtr = reinterpret_cast<char*>(data);
    std::ofstream file (fileName, std::ios::out | std::ios::binary);
    if(!file.is_open()){
        std::cout << "Cache file error!" << std::endl;
        std::cout << "file:\"" << fileName << "\"" << std::endl;
        return false;
    }
    file.write(data_chPtr, length);
    file.close();
    return true;
}

bool EmCache::save(arma::mat &data){
    if(!en_flag){
        LOG_ERR("cache disable, save error\n");
        return false;
    }
    // 缓存矩阵数据到文件
    bool res = data.save(fileName, arma::arma_binary);
    return res;
}

bool EmCache::load(arma::mat &target_mat){
    if(!en_flag){
        LOG_ERR("cache disable, load error\n");
        return false;
    }
    // 从缓存文件读矩阵
    if(isFileEmpty()){
        return false;
    }
    bool res = target_mat.load(fileName, arma::arma_binary);
    return res;

}

bool EmCache::isFileEmpty(){
    std::ifstream file(fileName);

    // 尝试打开文件并检查是否成功
    if (!file.is_open()) {
        std::cout << "Cache file absent: " << fileName << std::endl;
        return false;
    }

    // 获取文件大小
    file.seekg(0, std::ios::end);
    size_t fileSize = file.tellg();
    file.close();

    // 如果文件大小为0，则为空
    if (fileSize == 0) {
        return true;
    } else {
        return false;
    }

}


void EmCache::changeCacheFile(std::string fn){
    this->fileName = fn;
}

void EmCache::enable(){
    this->en_flag = 1;
}

void EmCache::disable(){
    this->en_flag = 0;
}
void EmCache::en_refresh(){
    this->refreshCache = 1;
}
void EmCache::dis_refresh(){
    this->refreshCache = 0;
}
std::string EmCache::get_cacheFile(){
    return this->fileName;
}
bool EmCache::is_enable(){
    return this->en_flag;
}
bool EmCache::is_refresh(){
    return this->refreshCache;
}
